package com.xgll.modules.admin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xgll.common.utils.PageUtils;
import com.xgll.common.utils.Query;
import com.xgll.common.utils.R;
import com.xgll.modules.admin.entity.CategoryEntity;
import com.xgll.modules.admin.service.CategoryService;

/**
 * Controller
 *
 * @author blowfish
 * @email lsc0630@tom.com
 * @date 2017-08-21 15:32:31
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查看列表
     */
    
    @RequestMapping("/list")
    @RequiresPermissions("category:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<CategoryEntity> categoryList = categoryService.queryList(query);
        int total = categoryService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(categoryList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("category:info")
    public R info(@PathVariable("id") Integer id) {
        CategoryEntity category = categoryService.queryObject(id);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.update(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("category:delete")
    public R delete(@RequestBody Integer[] ids) {
        categoryService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<CategoryEntity> list = categoryService.queryList(params);
        return R.ok().put("list", list);
    }

    /**
     * 查看信息(全部加载页面渲染太慢！)
     */
    @RequestMapping("/getAreaTree")
    public R getAreaTree() {
        List<CategoryEntity> list = categoryService.queryList(new HashMap<String, Object>());
        List<Map<String, Object>> tree=new ArrayList<>();
        Map<Integer, Object> parents=new HashMap<>();
        for (CategoryEntity sysRegionEntity : list) {        	
        	if(sysRegionEntity.getParentId()==0)
        	{
        		Map<String, Object> node=new HashMap<>();
            	Integer id=sysRegionEntity.getId();
            	node.put("value", id);
            	node.put("label", sysRegionEntity.getName());
        		tree.add(node);
        		parents.put(id, node);
        	}        	
        }
        for (CategoryEntity sysRegionEntity : list) {
        	Integer pid=sysRegionEntity.getParentId();
        	if(pid!=null){
        		Map<String, Object> node=new HashMap<>();
            	Integer id=sysRegionEntity.getId();
            	node.put("value", id);
            	node.put("label", sysRegionEntity.getName());
            	Map<String,Object> pnode=(Map)parents.get(sysRegionEntity.getParentId());
            	if(pnode==null) continue;
            	List<Map<String,Object>> children=(List)pnode.get("children");
            	if(children==null) {children=new ArrayList<>();pnode.put("children", children);}
            	children.add(node);
        	}
        	
        }
        return R.ok().put("node", tree);
    }

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping("/getCategorySelect")
    public R getCategorySelect() {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", "0");
        List<CategoryEntity> list = categoryService.queryList(map);
        return R.ok().put("list", list);
    }
}
